﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using DTC.Converters;
using DTC.Database;
using DTC.Popups;

namespace DTC
{
	/// <summary>
	/// Interaction logic for OrderPopup.xaml
	/// </summary>
	public partial class OrderPopup : Window
	{
		public OrderPopup()
		{
			this.InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            
            //
			
			// Insert code required on object creation below this point.
            order = new DTCOrder();
            order.DateFrom = App.DBI.GetLastOrderDate();
            order.DateTo = order.DateFrom.Value.AddMonths(1);
            order.StatusID = (int)DTCOrder.OrderStatuses.Added;
            isNew = true;
		    PrepareGrid();
		}

	    private void PrepareGrid()
	    {
	        patientOrders = order.DTCPatientOrders.ToList();
	        RequieredDrugsListBox.ItemsSource = patientOrders;
	        MainGrid.DataContext = order;
            view = CollectionViewSource.GetDefaultView(RequieredDrugsListBox.ItemsSource);
	        view.GroupDescriptions.Add(new PropertyGroupDescription("DTCDrugPrescription.DTCDrugUnit.DTCDrug"));
	    }

	    private ICollectionView view;
        private bool isNew;
        private DTCOrder order;
	    private List<DTCPatientOrder> patientOrders;
        public DTCOrder Order
        {
            get
            {
                return order;
            }
            set
            {
                if (order != value)
                {
                    RequieredDrugsListBox.ItemsSource = null;
                    order = value;
                    PrepareGrid();
                    isNew = false;
                }
            }
        }

        public OrderingSystem orderingSettings;

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (DialogResult == true)
            {
                if (RequieredDrugsListBox.Items.Count == 0)
                {
                    WarningPopup wp = new WarningPopup();
                    wp.Title = "Внимание!";
                    wp.Message = "Список заказанных препаратов не может быть пустым.";
                    wp.ShowDialog();
                    e.Cancel = true;
                    return;
                }
                if (isNew)
                    App.DBI.Db.DTCOrders.InsertOnSubmit(order);
                App.DBI.Db.SubmitChanges();
            }
            else
                App.DBI.ChangeCancel();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            new DTC.Resources.TextBoxBindingValidationSetter().PrepareBinding(MainGrid);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //if (order.StatusID == (int)DTCOrder.OrderStatuses.Changed)
            //{
                order.DTCPatientOrders.Clear();
                order.DTCOrderedBoxes.Clear();
            //}
            if (order.DateFrom != null && order.DateTo != null)
            {
                order.SetPatientsOrders();
            }
            PrepareGrid();
        }

        private void ListBox_Loaded(object sender, RoutedEventArgs e)
        {
            ListBox send = sender as ListBox;
            var boxcount = ((send.DataContext as CollectionViewGroup).Name) as DTCDrug;
            Binding myBinding = new Binding("TotalBoxCount");
            var conv = new BoxCountConverter();
            conv.DrugId = boxcount.DrugID;
            myBinding.NotifyOnSourceUpdated = true;
            myBinding.Converter = conv;
            myBinding.Source = Order;
            send.SetBinding(ListBox.ItemsSourceProperty, myBinding);
            //send.ItemsSource = order.TotalBoxCount.Where(b=>b.DTCBox.DTCDrugUnit.DTCDrug.DrugID == boxcount.DrugID);
        }

        private void PrintButton_Click(object sender, RoutedEventArgs e)
        {
            var pop = new PrintOrderPopup();
            pop.Order = this.Order;
            pop.ShowDialog();
        }
	}
}